package com.ztools.pc.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ztools.common.model.pc.sys.User;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.common.Result;
import com.ztools.common.util.pc.TokenAuthorization;
import com.ztools.pc.service.sys.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author:一口香菜
 * @Date: 2023/3/2  20:31
 * @Version 1.0
 */
@RequestMapping("/sys/user")
@RestController
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/list")
    @TokenAuthorization("/sys/user/search")
    public Result findList(@RequestBody PageUtil<User> pageUtil) {
        Page<User> list = userService.findList(pageUtil);
        return list != null ? Result.success("查询成功").data(list) : Result.error();
    }

    @RequestMapping("/delete")
    @TokenAuthorization("/sys/user/delete")
    public Result delete(@RequestBody User user) {
        boolean b = userService.delete(user);
        return b ? Result.success("删除成功") : Result.error();
    }

    @RequestMapping("/add")
    @TokenAuthorization("/sys/user/add")
    public Result add(@RequestBody User user) {
        boolean b = userService.add(user);
        return b ? Result.success("添加成功") : Result.error();
    }

    @RequestMapping("/update")
    @TokenAuthorization("/sys/user/update")
    public Result update(@RequestBody User user) {
        boolean b = userService.update(user);
        return b ? Result.success("修改成功") : Result.error();
    }

    @RequestMapping("/updatePwd")
    public Result updatePwd(@RequestBody User user) {
        boolean b = userService.updatePwd(user);
        return b ? Result.success("修改成功") : Result.error();
    }

    @GetMapping("/info")
    public Result info(String token) {
        User user = userService.findByToken(token);
        return user != null ? Result.success("获取用户信息成功").data(user) : Result.error("获取用户信息失败");
    }

    @RequestMapping("/findByObject")
    public Result findByObject(@RequestBody User user) {
        return Result.success().data(userService.findByObject(user));
    }

    @RequestMapping("/reset-password")
    public Result restPassword(@RequestBody User user) {
        boolean b = userService.restPassword(user);
        return b ? Result.success("密码重置成功") : Result.error("密码重置失败");
    }

}
